'use strict';
(function(){
    require(['jquery-1.10.2.min'],function(){
        require(['lib/top-4.9.min'],function(){
            var $html=$('html'),
                url,postData,param,isAct,cid,val,$parent,
                errorHtml = '<dl class="lg-web-error"><dt>Oops~网络错误</dt><dd>出现了网络错误，请检查您的网络是否正常⊙﹏⊙</dd></dl>',
                /* 首页轮播图 */
                $skSlider = $('#skSlider'),
                /* 加入队伍弹窗 */
                $contestApplyPop = $('#contestApplyPop'),
                $popJoinTeamLink = $('#popJoinTeamLink');
            
            function popChangeTeam(){//切换报名队伍方法
                $contestApplyPop.off('click','.js_pop_apply_team').on('click','.js_pop_apply_team',function(){
                    var $t = $(this);
                    url = $t.attr('action-url');
                    $t.addClass('active').siblings('.js_pop_apply_team').removeClass('active');
                    $popJoinTeamLink.attr('href',url);
                });
            };
            function eventScreenAct(){//筛选方法
                var $eventScreenForm=$('#eventScreenForm'),
                    $univsCheckBox=$('#univsCheckBox'),
                    $selectSchoolList=$('#selectSchoolList'),
                    $selectSchoolPop = $('#selectSchoolPop'),
                    $popMask=$('#popMask'),
                    $univsInp=$('#univsInp'),
                    $univsInpVal=$('#univsInpVal'),
                    val,cid,isChecked,uid;
                require(['dist/icheck.min'],function(){
                    require(['dist/sk.icheck.config.min'],function(){
                        $univsCheckBox.off('ifChecked').on('ifChecked',function(){
                            uid=$univsInp.attr('univs-id');
                            $univsInpVal.val(uid);
                            $eventScreenForm.submit();
                        });
                        $univsCheckBox.off('ifUnchecked').on('ifUnchecked',function(){
                            $univsInpVal.val(0);
                            $eventScreenForm.submit();
                        });
                    });
                });
                $selectSchoolList.off('click','.js_school_item').on('click','.js_school_item',function(){
                    var $t = $(this);
                    $selectSchoolList.find('li.js_school_item').removeClass('active');
                    $t.addClass('active');
                    val = $t.text();
                    uid = $t.attr('univs-id');
                    cid = '#'+$selectSchoolPop.attr('inp-target');
                    $(cid).val(val).attr('univs-id',uid).focus();
                    $selectSchoolPop.fadeOut();
                    $popMask.fadeOut();
                    isChecked=$univsCheckBox.prop('checked');
                    if(isChecked){//如果已经选中学校，直接筛选
                        $univsInpVal.val(uid);
                        $eventScreenForm.submit();
                    }
                });
            };
            popToggle.popOpen=function(){
                var self = this,
                    $html = $('html'),
                    popTarget,url,
                    $popMask = $('#popMask');
                $html.off('click','.js_pop_open').on('click','.js_pop_open',function(){
                    var $t = $(this);
                    url=$t.attr('action-url');
                    popTarget = $t.attr('pop-target');
                    $('#'+popTarget).fadeIn();
                    $popMask.fadeIn();
                    if(url){
                        if($t.attr('data_lock')){
                            return false;
                        }
                        $t.attr('data_lock',1);
                        $.ajax({
                            url : url,
                            dataType :'json',
                            success : function(r){
                                if(r.code == 0){
                                    if($('#'+popTarget+'Box')[0]){
                                        $('#'+popTarget+'Box')[0].innerHTML=r.data.html;
                                    }
                                }else{
                                    ajax_message(r.message);
                                }
                            },
                            error : function(){
                                ajax_message(errorHtml);
                            },
                            complete:function(){
                                $t.removeAttr('data_lock'); 
                            }
                        });
                    }
                    
                });
            };
            $(function(){
                if($skSlider){//判断是否使用轮播功能
                    require(['dist/sk.infinity.slider.min'],function(){
                        $skSlider.slider({
                            auto:true,
                            screenwidth:true,
                            time:5000
                        });//执行轮播
                    });
                }
                popChangeTeam();//执行切换报名队伍方法
                require(['dist/sk.selectschool.min'],function(){
                    selectSchoolAct();
                    eventScreenAct();
                });
            });
        });
    });
})();